package cn.com.jrj.base.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
public class DateUtil {
	
	private static final SimpleDateFormat ALL_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
	private static final SimpleDateFormat SHORT_FORMAT = new SimpleDateFormat("MM-dd");
	private static final SimpleDateFormat HHmm_FORMAT = new SimpleDateFormat("HH:mm");
	private static final DateUtil instance = new DateUtil();

	public static DateUtil getInstance() {
		return instance;
	}

	public String format(Date date, String format) {
		if (date == null)
			return null;
		try { 
			
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public String formatDate(Date date) {
		if (date == null)
			return null;
		try {
			return DATE_FORMAT.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public String formatTime(Date date) {
		if (date == null)
			return null;
		try {
			return TIME_FORMAT.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public String formatAll(Date date) {
		if (date == null)
			return null;
		try {
			return ALL_FORMAT.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public String formatShort(Date date) {
		if (date == null)
			return null;
		try {
			return SHORT_FORMAT.format(date);
		} catch (Exception e) {
			return null;
		}
	}
	
	public Date parse(String str, String format) {
		if (StringUtils.isEmpty(str))
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(str);
		} catch (Exception e) {
			return null;
		}
	}
	
	public String HHmmShort(Date date) {
		if (date == null)
			return null;
		try {
			return HHmm_FORMAT.format(date);
		} catch (Exception e) {
			return null;
		}
	}
	public String dayInWeek(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		int time = c.get(Calendar.DAY_OF_WEEK);
		String[] weekNames = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		String weekName = weekNames[time - 1];
		return weekName;
	}
}
